package com.jn.mvvm2.views;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.jn.mvvm2.R;
import com.jn.mvvm2.databinding.ActivityPetListBinding;
import com.jn.mvvm2.model.PetViewModel;

public class PetListActivity extends AppCompatActivity {
    private PetViewModel petViewModel;
    private PetAdapter petAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityPetListBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_pet_list);

        petViewModel = new ViewModelProvider(this).get(PetViewModel.class);
        binding.setViewModel(petViewModel);
        binding.setLifecycleOwner(this);

        petAdapter = new PetAdapter(pet -> {
            Intent intent = new Intent(PetListActivity.this, PetDetailActivity.class);
            intent.putExtra("pet", pet);
            startActivity(intent);
        });

        binding.petRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.petRecyclerView.setAdapter(petAdapter);

        String url = getIntent().getStringExtra("url");
        String type = getIntent().getStringExtra("type");
        String page = getIntent().getStringExtra("page");
        String num = getIntent().getStringExtra("num");
        String name = getIntent().getStringExtra("name");

        petViewModel.setUrl(url);
        petViewModel.fetchPets(type, page, num, name);
        petViewModel.getPets().observe(this, pets -> {
            if (pets != null) {
                petAdapter.submitList(pets);
            } else {
                Log.e("PetListActivity", "宠物列表为空");
            }
        });

        EditText searchEditText = findViewById(R.id.searchEditText);
        Button searchButton = findViewById(R.id.searchButton);

        searchButton.setOnClickListener(v -> {
            String query = searchEditText.getText().toString();
            petViewModel.searchPets(query); // 使用新的搜索功能
        });

        searchEditText.addTextChangedListener(new TextWatcher() {
            private static final long DELAY_MS = 500;
            private Runnable searchRunnable;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (searchRunnable != null) {
                    searchEditText.removeCallbacks(searchRunnable);
                }
                searchRunnable = () -> petViewModel.searchPets(s.toString());
                searchEditText.postDelayed(searchRunnable, DELAY_MS);
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }
}